<?php

namespace App\Admin\Controllers\Login;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Models\Admin\AdminUser;
use App\Admin\Validate\Manger\UserPost;
use Illuminate\Support\Facades\Redis;

class LoginController extends Controller
{

    /**
     * 超管登陆
     */
    public function dologin(Request $request)
    {
        try {
            $this->validate($request, [
                'username' => 'required',
                'password' => 'required',
            ]);
        } catch (\Exception $exception) {
            return response()->json(['code' => '001', 'msg' => '帐号或者密码不能为空']);
        }

        // $info = AdminUser::where('username', $request->username)->where('password', MD5(MD5('huakun_' . $request->password)))->first();
        if (strlen($request->password) != 32) {
            return response()->json(['code' => '001', 'msg' => '密码长度不规范']);
        }
        $info = AdminUser::where('username', $request->username)->where('password', MD5($request->password))->first();
        if ($info) {
            $admintoken = md5(time() . mt_rand(1000, 9999) . '__' . $info->id);
            Redis::set($admintoken, json_encode($info));
            $this->storeadmintokenkey($admintoken, $info->id);
            AdminUser::where(['id' => $info->id])->update(['lastlogin_ip' => getClientIp(), 'lastlogin_at' => time()]);
            return response()->json([
                'code' => '000',
                'msg' => '登录成功',
                'data' => ['admintoken' => $admintoken, 'info' => ['username' => $info->username, 'mobile' => $info->mobile, 'company_name' => $info->company_name]]
            ]);
        } else {
            return response()->json(['code' => '001', 'msg' => '帐号或者密码错误']);
        }
    }

    /**
     * 超管退出登录
     */
    public function dologinout(Request $request)
    {
        $admintoken = $request->header('admintoken');
        $memberinfo = Redis::get($admintoken);
        if (!$admintoken || !$memberinfo) {
            return response()->json(['code' => '001', 'msg' => '非法访问']);
        }
        $memberinfo = json_decode($memberinfo, true);
        $info = Adminuser::where(['id' => $memberinfo['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        Redis::delete($admintoken);
        $this->clearadmintokenkey($memberinfo['id']);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 超管更改密码
     */
    public function changepassword(Request $request)
    {
        $admintoken = $request->header('admintoken');
        $memberinfo = Redis::get($admintoken);
        if (!$admintoken || !$memberinfo) {
            return response()->json(['code' => '001', 'msg' => '非法访问']);
        }


        $memberinfo = json_decode($memberinfo, true);
        $info = Adminuser::where(['id' => $memberinfo['id']])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        $oldpassword = $request->oldpassword;
        $password = $request->password;
        $repassword = $request->repassword;
        if ((strlen($oldpassword) < 5 || strlen($oldpassword) > 20) || (strlen($password) < 5 || strlen($password) > 20) || (strlen($repassword) < 5 || strlen($repassword) > 20)) {
            return response()->json(['code' => '001', 'msg' => '请保持密码长度为5~20位']);
        }

        if ($oldpassword == '' || $password == '' || $repassword == '' || $password != $repassword) {
            return response()->json(['code' => '001', 'msg' => '请确保输入框完整']);
        }



        // echo MD5(MD5('huakun_'.$oldpassword));
        // echo $info->password;
        if (MD5(MD5('huakun_' . $oldpassword)) != $info->password) {
            return response()->json(['code' => '001', 'msg' => '原密码不正确']);
        }

        Adminuser::where(['id' => $memberinfo['id']])->update(['password' => MD5(MD5('huakun_' . $password))]);

        //清除缓存
        $this->clearadmintokenkey($memberinfo['id']);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 存储超管admintoken集合
     */
    protected function storeadmintokenkey($admintoken, $adminid)
    {
        Redis::set('admin_user_admintokenbyid_' . $adminid, $admintoken);
    }

    /**
     * 清除超管admintoken集合，常用语修改密码后
     */
    protected function clearadmintokenkey($adminid)
    {
        Redis::delete('admin_user_admintokenbyid_' . $adminid);
    }
}
